using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Acms.DataControls
{
    [Designer(typeof(TemplateFileDataSourceDesigner))]
    [ToolboxData("<{0}:TemplateFileDataSource runat=server/>")]
    public class TemplateFileDataSource : GenericDataSource<Bll.TemplateFile>
    {
        public enum Fields
        {
            Id,
            TemplateId,
            Name,
            Path,
            MasterPagePath,
            Theme,
            IsDefault,
            ConcurencyId
        }

        protected override Acms.Bll.TemplateFile PopulateData(UpdateAction action, System.Collections.IDictionary keys, System.Collections.IDictionary values, System.Collections.IDictionary oldValues)
        {
            Bll.TemplateFile tmp = null;
            byte[] concurencyId = null;
            if (action == UpdateAction.Insert)
            {
                tmp = Bll.TemplateFile.CreateInstance();
            }
            else
            {
                int id = GetValue<int>(Fields.Id, keys);
                concurencyId = GetValue<byte[]>(Fields.ConcurencyId, keys);
                tmp = Bll.TemplateFile.GetById(id);
            }

            if (tmp != null)
            {
                tmp.CheckConcurent(concurencyId);

                tmp.IsDefault = SetValue(tmp.IsDefault, Fields.IsDefault, values, oldValues);
                tmp.TemplateId = SetValue(tmp.TemplateId, Fields.TemplateId, values, oldValues);
                tmp.Name = SetValue(tmp.Name, Fields.Name, values, oldValues);
                tmp.Path = SetValue(tmp.Path, Fields.Path, values, oldValues);
                tmp.MasterPagePath = SetValue(tmp.MasterPagePath, Fields.MasterPagePath, values, oldValues);
                tmp.Theme = SetValue(tmp.Theme, Fields.Theme, values, oldValues);
            }

            return tmp;
        }

        protected override int SaveChanges(Acms.Bll.TemplateFile item)
        {
            item.Update();
            return 1;
        }

        protected override int Delete(Acms.Bll.TemplateFile item)
        {
            item.Delete();
            return 1;
        }

        protected override void AddInsertedKey(Acms.Bll.TemplateFile item, System.Collections.IDictionary values)
        {
            values[Fields.Id.ToString()] = item.Id;
        }
    }

    public class TemplateFileDataSourceDesigner : GenericDataSourceDesigner<Bll.TemplateFile>
    {
        protected override Acms.Bll.TemplateFile GetDesignTimeItem(int index)
        {
            Random rnd = new Random(index);
            string[] names = new string[] { "Default", "List", "Accessible", "AccessibleList", "SearchList" };
            string[] templateNames = new string[] { "Home", "Article", "Search" };
            string[] themes = new string[] { null, "special", "red" };
            string[] masters = new string[] { null, "~/alternative.master", "~/nomenu.master"};

            Bll.TemplateFile tmp = Bll.TemplateFile.CreateInstance();
            tmp.Name = names[rnd.Next(0, names.Length)];
            tmp.TemplateId = 1;
            tmp.IsDefault = rnd.Next(0, 1) == 1;
            tmp.Path = string.Format("~/Templates/{0}/{1}.ascx", templateNames[rnd.Next(0, templateNames.Length)], names[rnd.Next(0, names.Length)]);
            tmp.MasterPagePath = string.Format("~/{1}.master", masters[rnd.Next(0, masters.Length)]);
            return tmp;
        }
    }
}
